package com.gongpingjialoan.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.car.MainTainActivity;
import com.gongpingjialoan.activity.car.MyLikeCarFragment;
import com.gongpingjialoan.utility.DhUtil;

import java.text.DecimalFormat;

/**
 * Created by Administrator on 2015/11/2.
 */
public class ModifyMileDialog extends Dialog implements View.OnClickListener {


    private ImageView cancleButton;

    private TextView titleTextView , messageTextView;

    private EditText editText;

    private Button button;

    private Object object;

    private LinearLayout typeLinearLayout;

    private View lineView;

    private int type = 0;

    private String baoyang_type;

    private RadioGroup radioGroup;

    private int padding;

    private double miles;


    public ModifyMileDialog(Context context ,int miles, Object obj, int type) {
        super(context, R.style.dialogs);
        this.object = obj;
        this.type = type;
        if(miles == -1){
            this.miles = 100;
        }else {
            this.miles = miles / 10000;
        }
        padding = DhUtil.dip2px(context , 20);
    }

    public void setTitle(String title) {
        if (titleTextView != null) {
            titleTextView.setText(title);
        }
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modify_mile_layout);
        Window win = getWindow();
        win.getDecorView().setPadding(padding, 0, padding, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        win.setAttributes(lp);
        typeLinearLayout = (LinearLayout)this.findViewById(R.id.baoyang_type);
        editText = (EditText) this.findViewById(R.id.mile_edit);
        lineView = this.findViewById(R.id.line_type);
        messageTextView = (TextView) this.findViewById(R.id.message);
        button = (Button) this.findViewById(R.id.button);
        button.setOnClickListener(this);
        radioGroup = (RadioGroup)this.findViewById(R.id.group);
        cancleButton = (ImageView) this.findViewById(R.id.cancle);
        cancleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        titleTextView = (TextView) this.findViewById(R.id.title);

        if(type == 2){
            typeLinearLayout.setVisibility(View.GONE);
            lineView.setVisibility(View.GONE);
            titleTextView.setText("请选择填写保养信息");
            messageTextView.setText("上次保养里程");
        }else{
            typeLinearLayout.setVisibility(View.GONE);
            lineView.setVisibility(View.GONE);
        }

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId == R.id.big){
                    baoyang_type = "big";
                }else{
                    baoyang_type = "small";
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        String mile = editText.getText().toString();
        if (TextUtils.isEmpty(mile)) {
            Toast.makeText(getContext(), "请输入里程", Toast.LENGTH_SHORT).show();
            return;
        }
        String s_mile = "";
        if (mile.length() > 5) {
            Toast.makeText(getContext(),
                    "请输入正确的公里数，小数和整数均不能超过2位数!", Toast.LENGTH_SHORT)
                    .show();
            return;
        }


        try {
            Double distance = Double.parseDouble(mile);

            if(distance < 0  || distance > miles){
                Toast.makeText(getContext(), "请填写正确的行驶里程（0-"+ miles + "万公里）",
                        Toast.LENGTH_SHORT).show();
                return;
            }
            DecimalFormat df = new DecimalFormat("#0.00");
            s_mile = df.format(distance);
        } catch (Exception e) {
            Toast.makeText(getContext(), "请填写正确的行驶里程（0-"+ miles + "万公里）",
                    Toast.LENGTH_SHORT).show();
            return;
        }

        if (type == 0) {
            MyLikeCarFragment myLikeCarFragment = (MyLikeCarFragment) object;
            myLikeCarFragment.modifymiles(s_mile);
            dismiss();
        }else if(type == 1){
            MainTainActivity mainTainActivity = (MainTainActivity) object;
            mainTainActivity.modifyMile(s_mile);
            dismiss();
        }else if(type == 2){
            baoyang_type = "small";
            if(TextUtils.isEmpty(baoyang_type)){
                Toast.makeText(getContext(), "请选择保养类型!", Toast.LENGTH_SHORT).show();
                return;
            }
            MainTainActivity mainTainActivity = (MainTainActivity) object;
            mainTainActivity.havdMaintain(baoyang_type, s_mile);
            dismiss();
        }
    }
}
